package com.example.meitao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.meitao.R;
import com.example.meitao.adapter.DiscountAdapter;
import com.example.meitao.bean.Discount;
import com.example.meitao.config.NetConfig;
import com.example.meitao.task.DiscountNetTask;
import com.example.meitao.task.NetTask;
import com.example.meitao.ui.DetailActivity;
import com.example.meitao.ui.GoShopingActivity;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 星星 on 2016/8/29.
 */
public class DiscountFragment extends Fragment {
    private ListView mListView;
    private List<Discount.Databean.Toplistbean> data;
    private List<ImageView> imageViews;
    private ViewPager mViewPager;
    private PagerAdapter mAdapter;
    private BaseAdapter adapter;
    private List<Discount.Databean.Resultsbean> mResultsbean;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discount, null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent2 = new Intent(getActivity(), GoShopingActivity.class);
                startActivity(intent2);
            }
        });
    }

    private void setData() {
        adapter = new DiscountAdapter(mResultsbean,getActivity());
        mListView.setAdapter(adapter);
    }

    private void initData() {
        data = new ArrayList<>();
        mResultsbean = new ArrayList<>();
        new DiscountNetTask(){
            @Override
            public void onCallBack(Discount result) {
                mResultsbean.addAll(result.getData().getResults());
            }
        }.execute(NetConfig.DISCOUNT_PATH);
        imageViews = new ArrayList<>();

        new NetTask(){
            @Override
            public void onCallBack(String result) {
                JSONObject object = null;
                try {
                    object = new JSONObject(result);
                    JSONObject object1 = object.getJSONObject("data");
                    JSONArray toplist = object1.getJSONArray("toplist");
                    for (int i = 0; i < toplist.length(); i++) {
                        final ImageView imageView  = new ImageView(getActivity());
                        JSONObject object2 = toplist.getJSONObject(i);
                        final String image = object2.getString("pic");
                        Picasso.with(getActivity()).load(image).into(imageView);
                        imageViews.add(imageView);
                        //设置Page间间距
                        mViewPager.setPageMargin(20);
                        //设置缓存的页面数量
                        mViewPager.setOffscreenPageLimit(3);
                        mViewPager.setAdapter(mAdapter = new PagerAdapter()
                        {
                            @Override
                            public Object instantiateItem(ViewGroup container, final int position)
                            {
                                final ImageView view = new ImageView(getActivity());
                                new DiscountNetTask(){
                                    @Override
                                    public void onCallBack(Discount result) {
                                        Picasso.with(getActivity()).load(result.getData()
                                                .getToplist().get(position).getPic()).into(view);
                                        final int entity_id = result.getData().getToplist().get(position).getEntity_id();
                                        final Discount.Databean.Toplistbean toplistbean = result.getData().getToplist().get(position);
                                        view.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                Intent intent = new Intent(getActivity(),DetailActivity.class);
                                               // intent.putExtra("id",toplistbean.getEntity_id());
                                                getActivity().startActivity(intent);
                                            }
                                        });
                                    }
                                }.execute(NetConfig.DISCOUNT_PATH);

                                container.addView(view);
                                return view;
                            }

                            @Override
                            public void destroyItem(ViewGroup container, int position, Object object)
                            {
                                container.removeView((View) object);
                            }

                            @Override
                            public int getCount()
                            {
                                return imageViews.size();
                            }

                            @Override
                            public boolean isViewFromObject(View view, Object o)
                            {
                                return view == o;
                            }
                        });
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                            }
        }.execute(NetConfig.DISCOUNT_PATH);
    }

    private void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.lv_discount_show);
        View headView = View.inflate(getActivity(),R.layout.headview_discount,null);
        mViewPager = (ViewPager) headView.findViewById(R.id.vp_discount_viewpager);
        mListView.addHeaderView(headView);
    }


}